<?php
/*************************************************************************
Class Pick_Dao_ProductCategorySub
----------------------------------------------------------------
Derasoft CMS Project
Company: Pick Co., Ltd                                  
Email: admin@pick.vn                                    
Last updated: 08/08/2011
**************************************************************************/
class Pick_Dao_ProductCategorySub extends Pick_Core_Objects {
	function Pick_Dao_ProductCategorySub($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->typeId = " AND typeid = 2";		
		$this->objectInfo = "Pick_Dao_ProductCategorySubInfo";
		$this->table = DB_PREFIX."product_category";	
	}
	function saveCategorySubForProduct($productid, $arrCId){
		$this->cleanTrash("productid=".$productid);
		// Add new data into n_product_cateogry table
		foreach($arrCId as $itemCid){
			$arrField = array("productid"=>$productid, "categoryid"=>$itemCid,  "typeid"=>2);
			$this->addData($arrField);
		}
		return true;
	}
	
	// $condition maybe productid or categoryid in n_product_category table	
	function getArrIdProducts($condition){
		$arrProductCategorySub = $this->getObjects(1, $condition, "", 0);
		if(isset($arrProductCategorySub['data'])){	
			$arrIdProducts = array();
			foreach($arrProductCategorySub['data'] as $key=>$objProductCategorySub ){
				$arrIdProducts[] = $objProductCategorySub->getField('productid'); 
			}
			return $arrIdProducts;
		}
		return;
	}
	function getObjectProductsNews($page = 1, $condition = '1>0', $sort = array(), $items_per_page = 0) {
		$arrProducts = array();
		$news = new Pick_Dao_News();
		$arrProductCategorySub = $this->getObjects(1, $condition, $sort, 0);
		$arrIdProducts = $this->getArrIdProducts($condition);
		if(count($arrIdProducts)){
			return $arrProducts = $news->getObjects($page, "id IN(".implode(",", $arrIdProducts).")","", $items_per_page);
		}
		return;
	}
	function getObjectProductsProduct($page = 1, $condition = '1>0', $sort = array(), $items_per_page = 0) {
		$arrProducts = array();
		$product = new Pick_Dao_Product();
		$arrProductCategorySub = $this->getObjects(1, $condition, $sort, 0);
		$arrIdProducts = $this->getArrIdProducts($condition);
		if(count($arrIdProducts)){
			return $arrProducts = $product->getObjects($page, "id IN(".implode(",", $arrIdProducts).")","", $items_per_page);
		}
		return;
	}	
}